import { DataTypes, Model, Optional } from 'sequelize';
import sequelize from '../db';

export interface OssConfigAttributes {
  id: string;
  tenantId?: string;
  name: string;
  type: 'minio' | 'oss' | 's3' | 'local';
  endpoint: string;
  port: number;
  useSsl: boolean;
  accessKey: string;
  secretKey: string;
  bucketName: string;
  region?: string;
  isDefault: boolean;
  isActive: boolean;
  maxFileSize: number;
  allowedTypes?: string;
  remark?: string;
  createdAt: Date;
  updatedAt: Date;
}

interface OssConfigCreationAttributes
  extends Optional<
    OssConfigAttributes,
    'id' | 'tenantId' | 'region' | 'isDefault' | 'isActive' | 'maxFileSize' | 'allowedTypes' | 'remark' | 'createdAt' | 'updatedAt'
  > {}

class OssConfig extends Model<OssConfigAttributes, OssConfigCreationAttributes> implements OssConfigAttributes {
  public id!: string;
  public tenantId?: string;
  public name!: string;
  public type!: 'minio' | 'oss' | 's3' | 'local';
  public endpoint!: string;
  public port!: number;
  public useSsl!: boolean;
  public accessKey!: string;
  public secretKey!: string;
  public bucketName!: string;
  public region?: string;
  public isDefault!: boolean;
  public isActive!: boolean;
  public maxFileSize!: number;
  public allowedTypes?: string;
  public remark?: string;
  public readonly createdAt!: Date;
  public readonly updatedAt!: Date;
}

OssConfig.init(
  {
    id: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true,
    },
    tenantId: {
      type: DataTypes.UUID,
      allowNull: true,
      field: 'tenant_id',
    },
    name: {
      type: DataTypes.STRING(100),
      allowNull: false,
    },
    type: {
      type: DataTypes.STRING(50),
      allowNull: false,
      defaultValue: 'minio',
    },
    endpoint: {
      type: DataTypes.STRING(255),
      allowNull: false,
    },
    port: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 443,
    },
    useSsl: {
      type: DataTypes.BOOLEAN,
      defaultValue: true,
      field: 'use_ssl',
    },
    accessKey: {
      type: DataTypes.STRING(255),
      allowNull: false,
      field: 'access_key',
    },
    secretKey: {
      type: DataTypes.STRING(255),
      allowNull: false,
      field: 'secret_key',
    },
    bucketName: {
      type: DataTypes.STRING(100),
      allowNull: false,
      field: 'bucket_name',
    },
    region: {
      type: DataTypes.STRING(100),
      allowNull: true,
      defaultValue: 'us-east-1',
    },
    isDefault: {
      type: DataTypes.BOOLEAN,
      defaultValue: false,
      field: 'is_default',
    },
    isActive: {
      type: DataTypes.BOOLEAN,
      defaultValue: true,
      field: 'is_active',
    },
    maxFileSize: {
      type: DataTypes.INTEGER,
      defaultValue: 50,
      field: 'max_file_size',
    },
    allowedTypes: {
      type: DataTypes.TEXT,
      allowNull: true,
      field: 'allowed_types',
    },
    remark: {
      type: DataTypes.STRING(500),
      allowNull: true,
    },
    createdAt: {
      type: DataTypes.DATE,
      allowNull: false,
      field: 'created_at',
    },
    updatedAt: {
      type: DataTypes.DATE,
      allowNull: false,
      field: 'updated_at',
    },
  },
  {
    sequelize,
    tableName: 'oss_configs',
    timestamps: true,
    underscored: true,
  }
);

export default OssConfig;

